Arrays
An array is a collection of variables of the same type arranged in the memory in an uniform order.
Types of arrays
Syntax:1) datatype []arrayname;
Arrayname=new datatype[size];
2) datatype []arrayname=new datatype[size];
3) datatype []arrayname={values};
4) datatype [,]arrayname=new datatype[size,size];
5) datatype[][]arrayname=new datatype[size];
Program on Single dimensional arrays
class sample
{
static void Main(string[] args)
{
int []a;
a = new int[5];
int[] b = new int[5];
int[] c = { 10, 20, 34, 54 ,6};
for (int i = 0; i < 5; i++)
{
a[i] = i + 10;
b[i] = a[i] / 3;
}
for (int i = 0; i < 5; i++)
{
Console.WriteLine(a[i] + " " + b[i]+" "+c[i]);
}
}
}
Note: Do some more programs on single dimensional arrays
Program on Double dimensional Arrays
class sample
{
static void Main(string[] args)
{
int[,] a = new int[2, 2];
int[,] b = new int[2, 2];
int[,] c = new int[2, 2];
int i, j;
for (i = 0; i < 2; i++)
{
for (j = 0; j < 2; j++)
{
Console.WriteLine("Enter a matrics");
a[i, j] = Convert.ToInt32(Console.ReadLine());
}
}
for (i = 0; i < 2; i++)
{
for (j = 0; j < 2; j++)
{
Console.WriteLine("Enter B matrics");
b[i, j] = Convert.ToInt32(Console.ReadLine());
}
}
for (i = 0; i < 2; i++)
{
for (j = 0; j < 2; j++)
{
c[i, j] = a[i, j] + b[i, j];
Console.Write(c[i, j] + " ");
}
Console.WriteLine();
}
}
}